package nothing.model;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.appfuse.model.BaseObject;
import org.appfuse.model.User;

@Entity
@Table(name = "ingredient")
public class Ingredient extends BaseObject {

    private Long id;
    private Recipe recipe;
    private Food food;
    private BigDecimal qty;
    private Measurement measurement;
    private User user;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recipe_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Recipe getRecipe() {
        return recipe;
    }

    public void setRecipe(Recipe recipe) {
        this.recipe = recipe;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "food_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    @Column(name = "qty", length = 6)
    public BigDecimal getQty() {
        return qty;
    }

    public void setQty(BigDecimal qty) {
        this.qty = qty;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "measurement_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Measurement getMeasurement() {
        return measurement;
    }

    public void setMeasurement(Measurement measurement) {
        this.measurement = measurement;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((food == null) ? 0 : food.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((measurement == null) ? 0 : measurement.hashCode());
        result = prime * result + ((qty == null) ? 0 : qty.hashCode());
        result = prime * result + ((recipe == null) ? 0 : recipe.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Ingredient other = (Ingredient) obj;
        if (food == null) {
            if (other.food != null) {
                return false;
            }
        } else if (!food.equals(other.food)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (measurement == null) {
            if (other.measurement != null) {
                return false;
            }
        } else if (!measurement.equals(other.measurement)) {
            return false;
        }
        if (qty == null) {
            if (other.qty != null) {
                return false;
            }
        } else if (!qty.equals(other.qty)) {
            return false;
        }
        if (recipe == null) {
            if (other.recipe != null) {
                return false;
            }
        } else if (!recipe.equals(other.recipe)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Ingredient{" +
                "id=" + id + ", " +
                "recipe=" + recipe.getName() + ", " +
                "food=" + food.getName() + ", " +
                "qty=" + qty + ", " +
                //"measurement=" + measurement.getName() +
                measurement.getName() +
                "}";
    }
}
